package com.kinon.norogamilk.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.bean.RaodBean;

import java.util.ArrayList;

/**
 * Created by lhqq on 2018-01-18.
 */

public class RecyclRaodAdapter extends RecyclerView.Adapter<RecyclRaodAdapter.ViewHolder> {

    private ArrayList<RaodBean> list;
    private Context mContext;
    private LayoutInflater inflater;
    private int lastPosition = 0;
    private OnMyItemClickListener mOnItemClickListener = null;

    public RecyclRaodAdapter(ArrayList<RaodBean> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    public void setOnItemClickListener(OnMyItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_road_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv_raodName.setText("货道" + (position + 1));
        if (lastPosition == position) {
            holder.tv_raodName.setChecked(true);
            mOnItemClickListener.onItemClick(holder.tv_raodName, position);
        } else {
            holder.tv_raodName.setChecked(false);
        }

        holder.tv_raodName.setOnClickListener(new MyItemClickListener(holder.tv_raodName, position));

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setLastPosition(int lastPosition) {
        this.lastPosition = lastPosition;
        notifyDataSetChanged();
    }

    public interface OnMyItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        RadioButton tv_raodName;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_raodName = itemView.findViewById(R.id.tv_roadName);
        }
    }

    private class MyItemClickListener implements View.OnClickListener {
        RadioButton view;
        int position;

        public MyItemClickListener(RadioButton view, int position) {
            this.view = view;
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            lastPosition = position;
            notifyDataSetChanged();


        }

        private void intentFoodActivity(int type) {

        }
    }

}
